﻿{% extends "vmServer/index_server.html" %}

{% block content %}


						<div class="row">
							
							<div class="col-sm-3">
								<div class="widget-box">
									<div class="widget-header widget-header-flat">
										<h4>Tips</h4>
									</div>

									<div class="widget-body">
										<div class="widget-main">
											<ul class="list-unstyled spaced2">
												<li>
													<i class="icon-circle green"></i>
														平台提供三种方式创建虚拟机，自定义，模板以及XML配置
												</li>
							
												<li class="muted">
													<i class="icon-warning-sign orange"></i>
													新建虚拟机请注意一下几点：
																								
												</li>
						
												<li>
													<ul class="list-unstyled">
														<li>
															<i class="icon-caret-right blue"></i>
															配置存储池：平台使用存储池管理虚拟机镜像资源
														</li>
						
															<li>
																<i class="icon-caret-right blue"></i>
															配置网络：如果不需要网络可以不配置网络
														</li>
						
													</ul>
												</li>																																																																																																														
											</ul>													
										</div>
									</div>
								</div>
							</div>								

									<div class="col-sm-6 widget-container-span">
										<div class="widget-box transparent">
											<div class="widget-header">
												<h4 class="lighter">
													<i class="icon-cogs"></i>
													创建虚拟机
												</h4>

												<div class="widget-toolbar no-border">
													<ul class="nav nav-tabs" id="myTab2">
														<li class="active">
															<a data-toggle="tab" href="#home2">自定义</a>
														</li>

														<li>
															<a data-toggle="tab" href="#profile2">模板</a>
														</li>

														<li>
															<a data-toggle="tab" href="#info2">XML</a>
														</li>
													</ul>
												</div>
											</div>

											<div class="widget-body">
												<div class="widget-main padding-12 no-padding-left no-padding-right">
													<div class="tab-content padding-4">
														<div id="home2" class="tab-pane in active">
														<h4 class="header green">自定义配置</h4>
															<div class="slim-scroll" data-height="100">
																<form class="form-horizontal" role="form" id="addInstance">
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-1">
																	<i class="icon-user bigger-110 green"></i>
																		 <strong>
																		 	宿主机
																		 </strong> 
																	</label>
							
																	<div class="col-sm-9">
																		<input class="input-sm" type="text"   value="{{ vmServer.server_ip }}" disabled/>	
																	</div>
																	<div class="col-sm-9" style="display: none;">
																		<input class="input-sm" type="text" name="server_id"   value="{{ vmServer.id }}"/>	
																	</div>											
																</div>
							
																<div class="space-4"></div>
							
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-3">
																	<i class="icon-edit bigger-110 green"></i>
																	<strong>名称</strong>
																	</label>
							
																	<div class="col-sm-9">
																		<input class="input-sm" type="text" name="vm_name" placeholder="虚拟机名称" value=""/>											
																		<div class="space-2"></div>									
																	</div>
																</div>

																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="storage-install-type">
																		<strong>系统磁盘</strong>
																	</label>
																	<div class="col-sm-9">
																		<select id="storage-install-type" name="storagetype">
																			<option value="new">创建新的磁盘</option>
																			<option value="existing">选择现有磁盘</option>
																		</select>
																	</div>
																</div>
																
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="vStorage-select">
																		<strong>存储池</strong>
																	</label>
																	<div class="col-sm-9">
																		<select id="vStorage-select" name="storage">
																			<option value="">选择存储池</option>
																			{% for ds in vStorage %}
																				<option value="{{ds.pool_name}}">{{ds.pool_name}}</option>
																			{% endfor %}
																		</select>											
																	</div>
																</div>
							
																<div class="space-4"></div>
							
																<div class="form-group" id="field-system-select">
																	<label class="col-sm-3 control-label no-padding-right" for="system-select">
																		<strong> 操作系统安装盘</strong>
																	</label>
																	<div class="col-sm-9">
																		<select id="system-select" name="system">
																			<option value="">选择操作系统安装盘</option>
																			{% for ds in vMImages %}
																				<option value="{{ds.vol_path}}">{{ds.vol_name}}</option>
																			{% endfor %}											
																		</select>	
																	</div>
																</div>

																<div class="form-group" id="field-disk-select">
																	<label class="col-sm-3 control-label no-padding-right" for="disk-select">
																		<strong>磁盘镜像文件</strong>
																	</label>
																	<div class="col-sm-9">
																		<select id="disk-select" name="vol">
																			<option value="">选择磁盘镜像</option>
																			{% for ds in diskImages %}
																				<option value="{{ds.vol_path}}">{{ds.vol_name}}</option>
																			{% endfor %}
																		</select>
																	</div>
																</div>
									
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="owner-select">
																		<strong>所有者</strong>
																	</label>
																	<div class="col-sm-9">
																		<select id="owner-select" name="owner">
																			<option value="">选择用户</option>
																			{% for ds in userList %}
																				<option value="{{ds.username}}">{{ds.username}}</option>
																			{% endfor %}											
																		</select>	
																	</div>
																</div>

																<hr>									
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-3"><strong>CPU</strong></label>
							
																	<div class="col-sm-9">
																		<input class="input-sm" type="text" id="form-field-3" placeholder="个数" name="cpu" />											
																		<div class="space-2"></div>
																		<div class="help-block" id="input-size-slider-3"></div>										
																	</div>
																</div>									
																
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-4"><strong>内存</strong></label>
																	<div class="col-sm-9">
																		<input class="input-sm" type="text" id="form-field-4" placeholder="单位MB" name="mem" />											
																		<div class="space-2"></div>
																		<div class="help-block" id="input-size-slider-4"></div>										
																	</div>
																</div>
							
																<div class="form-group" id="field-disk-size">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-5"><strong>硬盘</strong></label>
							
																	<div class="col-sm-9">
																		<input class="input-sm" type="text" id="form-field-5" placeholder="单位GB" name="disk" />											
																		<div class="space-2"></div>
																		<div class="help-block" id="input-size-slider-5"></div>										
																	</div>
																</div>

																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-4"><strong>接入网络</strong></label>
							
																	<div class="col-sm-9">
																		<select multiple class="width-20 chosen-select" id="netk"  data-placeholder="网络选择">	
																			<option value=""></option>
																			{% for ds in netkList %}			
																				<option value="{{ds.name}}">{{ds.name}}</option>
																			{% endfor %}
																		</select>	
																																						
																	</div>
																</div>

																<div class="hr hr32 hr-dotted"></div>
																<div class="form-group">
																	<div class="col-md-offset-3 col-md-9">
																		<button class="btn btn-info" type="button" onclick="addVmInstance(this,'custom')">
																			<i class="icon-ok bigger-110"></i>
																			提交
																		</button>
																		<button class="btn" type="reset">
																			<i class="icon-undo bigger-110"></i>
																			撤销
																		</button>
																	</div>
																</div>						
															</form>	
															</div>
														</div>

														<div id="profile2" class="tab-pane">
															<h4 class="header green">
																<button class="btn btn-sm btn-success" data-toggle="modal" data-target="#tempModal">
																	<i class="icon-plus"></i>
																	模板创建
																</button>
															</h4>
															<div class="slim-scroll" data-height="100">
																<div class="table-responsive">
																	
																	
															<table class="table table-striped table-bordered">
															<thead>
																<tr>
																	<th class="center">序号</th>
																	<th class="center">模板名称</th>
																	<th class="hidden-xs center">CPU</th>
																	<th class="hidden-480 center">内存(MB)</th>
																	<th class="center">系统盘(GB)</th>
																	
																</tr>
															</thead>
										
															<tbody>
																{% for ds in tempList %}
																<tr>
																	<td class="center">{{ forloop.counter }}</td>
																	<td class="center">
																		<a>{{ ds.name }}</a>
																	</td>
																		<td class="hidden-xs center">
																			{{ ds.cpu }}									
																	</td>
																	<td class="center">
																			{{ ds.mem }}																		
																	</td>
																	<td class="center">
																		{{ ds.disk }}											
																	</td>
																	
																</tr>
																{% endfor %}
															</tbody>
														</table>
																	
																</div>
															</div>
														</div>

														<div id="info2" class="tab-pane">
															
																	<h4 class="header green">XML Editor</h4>
							
																				<textarea class="span12" id="instance_xml"  cols="114" rows="40">																																											
<domain type='kvm'>
  <name>linux-001</name>
  <memory unit='KiB'>1905664</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <memtune>
    <hard_limit unit='KiB'>1905664</hard_limit>
    <soft_limit unit='KiB'>1905664</soft_limit>
  </memtune>
  <vcpu placement='static' current='2'>32</vcpu>
  <os>
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
	<bootmenu enable='yes' timeout='3000'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='localtime'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/data/vmdisk/demo.img'/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/data/iso/CentOS-6.3-x86_64-bin-DVD1.iso'/>
      <target dev='hda' bus='ide' tray='open'/>
      <readonly/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='usb' index='0'>
      <alias name='usb0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </memballoon>
  </devices>
</domain>
																																										
																				</textarea>
																			
							
																			<div class="widget-toolbox padding-4 clearfix">
																				<div class="btn-group pull-left">
																					<button class="btn btn-sm btn-info">
																						<i class="icon-remove bigger-125"></i>
																						撤销
																					</button>
																				</div>
							
																				<div class="btn-group pull-right">
																					<button class="btn btn-sm btn-purple" onclick="addXmlInstace(this,{{ vmServer.id }},'xml')">
																						<i class="icon-save bigger-125"></i>
																						添加
																					</button>
																				</div>
																			</div>

														</div>
													</div>
												</div>
											</div>
										</div>
									</div>
									
									
				
				<!-- 模态框（Modal） -->
				<div class="modal fade" id="tempModal" tabindex="-1" role="dialog" aria-labelledby="tempModalLabel" aria-hidden="true">
					<div class="modal-dialog" style="width:800px;">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
									&times;
								</button>
								<h4 class="modal-title" id="tempModalLabel">
									{{ vmServer.hostname }}添加主机
								</h4>
							</div>
							<div class="modal-body">
								<form class="form-horizontal" role="form" id="tempAddInstance">
																<div class="form-group">
																	<div class="col-sm-9" style="display: none;">
																		<input class="input-sm" type="text" name="server_id"   value="{{ vmServer.id }}"/>	
																	</div>											
																</div>
							
																<div class="space-4"></div>
							
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-input-readonly">																	
																	<strong> 虚拟机模板</strong> </label>
							
																	<div class="col-sm-9">
																		<select name="temp">
																			<option value="">选择模板</option>
																			{% for ds in tempList %}
																				<option value="{{ds.id}}">{{ds.name}}</option>
																			{% endfor %}											
																		</select>	
																	</div>
																</div>	
							
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-3">																	
																	<strong>虚拟机名称</strong>
																	</label>
							
																	<div class="col-sm-9">
																		<input class="input-sm" type="text" name="vm_name" placeholder="虚拟机名称" value=""/>											
																		<div class="space-2"></div>									
																	</div>
																</div>
																
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-field-2"> 																	
																	<strong>存储池</strong>
																	</label>
							
																	<div class="col-sm-9">
																		<select id="vStorage-select" name="storage">
																			<option value="">选择存储池</option>
																			{% for ds in vStorage %}
																				<option value="{{ds.pool_name}}">{{ds.pool_name}}</option>
																			{% endfor %}
																		
																		</select>											
																	</div>
																</div>
							
																<div class="space-4"></div>
							
																<div class="form-group">
																	<label class="col-sm-3 control-label no-padding-right" for="form-input-readonly">																	
																	<strong> 操作系统安装盘1</strong> </label>
							
																	<div class="col-sm-9">
																		<select id="system-select" name="system">
																			<option value="">选择操作系统安装盘</option>
																			{% for ds in vMImages %}
																				<option value="{{ds.vol_path}}">{{ds.vol_name}}</option>
																			{% endfor %}											
																		</select>	
																	</div>
																</div>																							
																<hr>
																
																						
															</form>
							</div>
							<div class="modal-footer">
								<button type="button" class="btn btn-default" data-dismiss="modal">关闭
								</button>
								<button type="button" class="btn btn-primary" onclick="addTempInstace(this,'template')">
									添加
								</button>
							</div>
						</div><!-- /.modal-content -->
					</div><!-- /.modal -->
				</div>
									


			</div><!-- /.row -->
						
						
						
		<script src="/static/js/markdown/bootstrap-markdown.min.js"></script>
		<script type="text/javascript">
			
		   
			jQuery(function($) {
			
				$(".chosen-select").chosen(); 
				$('#chosen-multiple-style').on('click', function(e){
					var target = $(e.target).find('input[type=radio]');
					var which = parseInt(target.val());
					if(which == 2) $('#form-field-select-4').addClass('tag-input-style');
					 else $('#form-field-select-4').removeClass('tag-input-style');
				});
			
			
				$( "#input-size-slider-3" ).css('width','200px').slider({
					value:1,
					range: "min",
					min: 1,
					max: 8,
					step: 1,
					slide: function( event, ui ) {
						var sizing = ['1', '2', '4', '6', '8', '12', '16', '24', '32'];
						var val = parseInt(ui.value);
						$('#form-field-3').attr('class', sizing[val]).val(sizing[val]);
					}
				});
			
				$( "#input-size-slider-4" ).css('width','200px').slider({
					value:1,
					range: "min",
					min: 1,
					max: 8,
					step: 1,
					slide: function( event, ui ) {
						var sizing = ['128', '512', '1024', '2048', '2048', '4096', '8192', '16384', '32768'];
						var val = parseInt(ui.value);
						$('#form-field-4').attr('class', sizing[val]).val(sizing[val]);
					}
				});
			
				$( "#input-size-slider-5" ).css('width','200px').slider({
					value:1,
					range: "min",
					min: 1,
					max: 8,
					step: 1,
					slide: function( event, ui ) {
						var sizing = ['10', '20', '30', '40', '50', '60', '80', '100', '120'];
						var val = parseInt(ui.value);
						$('#form-field-5').attr('class', sizing[val]).val(sizing[val]);
					}
				});
				
				$('table th input:checkbox').on('click' , function(){
					var that = this;
					$(this).closest('table').find('tr > td:first-child input:checkbox')
					.each(function(){
						this.checked = that.checked;
						$(this).closest('tr').toggleClass('selected');
					});
						
				});

				
				$( "#input-size-slider-6" ).css('width','200px').slider({
					value:1,
					range: "min",
					min: 1,
					max: 8,
					step: 1,
					slide: function( event, ui ) {
						var sizing = ['1','2', '5', '10', '20', '30', '50', '100', '100'];
						var val = parseInt(ui.value);
						$('#form-field-6').attr('class', sizing[val]).val(sizing[val]);
					}
				});


				$('#storage-install-type').on('change', function(){
					var installType = $(this).val();
					if(installType == "new") {
						$("#field-system-select").show();
						$("#field-disk-select").hide();
						$("#field-disk-size").show();
					} else if(installType == "existing") {
						$("#field-system-select").hide();
						$("#field-disk-select").show();
						$("#field-disk-size").hide();
					}
				});
				$('#storage-install-type').trigger('change');

				$('#vStorage-select').on('change', function(){
					var poolName = $('#vStorage-select').val();
					$.ajax({
						dataType: "JSON",
						url:'/api/vmstorage/{{ vmServer.id }}/' + poolName,
						type:"GET",
						success:function(response){
							var poolVols = response.pool_vols;
							$('#disk-select option[value!=""]').remove();
							poolVols.forEach(function(poolVol) {
								if(poolVol.vol_type == 'qcow2' || poolVol.vol_type == 'raw') {
									$('#disk-select').append("<option value='" + poolVol.vol_name + "'>" + poolVol.vol_name + "</option>")
								}
							});
						}
					})
				});
				$('#vStorage-select').trigger('change');
			});
			

			function addTempInstace(obj,op) {
				var btnObj = $(obj);
				btnObj.attr('disabled',true);
				var required = ['vm_name','server_id','temp','system',''];
				var server_data = {};
				var form = document.getElementById('tempAddInstance');
				for (var i = 0; i < form.length; ++i) {
					var name = form[i].name;
					var value = form[i].value;	
					idx = $.inArray(name, required);	
					if (idx >= 0 && value.length == 0){
						btnObj.removeAttr('disabled');
						window.wxc.xcConfirm("请注意必填项不能为空~", window.wxc.xcConfirm.typeEnum.error);
						return false;
					}
					else if(  value.length != 0 && name.length != 0 ){
						server_data[name] = value;
					};				
				};			
				server_data['op'] = 'template';
				$.ajax({
					dataType: "JSON",
					url:'/addInstance/', //请求地址
					type:"POST",  //提交类似
					data:server_data,  //提交参数
					success:function(response){
						btnObj.removeAttr('disabled');
						if (response["code"] == 200){
							window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);
						}
						else {
							window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
						}
						
					},
			    	error:function(response){
			    		btnObj.removeAttr('disabled');
			    		window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
			    	}
				})	
			}

			function addXmlInstace(obj,server_id,op){
				var btnObj = $(obj);
				btnObj.attr('disabled',true);
				$.ajax({
					url:'/addInstance/', //请求地址
					type:"POST",  //提交类似
					data:{
						"server_id":server_id,
						"xml":$("#instance_xml").val(),
						"op":op
					},
					success:function(response){
						btnObj.removeAttr('disabled');
						if (response["code"] == 200){
							window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);
						}
						else {
							window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
						}												
					},
					error:function(response){
						btnObj.removeAttr('disabled');
			    		window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
			    	}
				})
			}

			//创建实例
			function addVmInstance(obj,op) {
				var btnObj = $(obj);
				btnObj.attr('disabled',true);

				var server_data = {};
				var required = ['vm_name','cpu',"mem","storage"]; /* 必填项列表 */
				if($("#storage-install-type").val() == 'new') {
					required.push("disk");
					required.push("system");
				}

				var form = document.getElementById('addInstance');
				for (var i = 1; i < form.length; ++i) {
					var name = form[i].name;
					var value = form[i].value;	
					idx = $.inArray(name, required);						
					if (idx >= 0 && value.length == 0){
						window.wxc.xcConfirm("请注意必填项不能为空~"+name, window.wxc.xcConfirm.typeEnum.error);
						btnObj.removeAttr('disabled');
						return false;
					};	
					if (  value.length != 0 && name.length != 0 ){
						server_data[name] = value;
					};					
				};

				var netkValues = [];    
				 $("#netk :selected").each(function(){
					 netkValues += $(this).val() +  ','
				 });	
				if (netkValues.length > 0){
					server_data.netk = netkValues.substring(0,netkValues.length-1);
				};

				server_data['op'] = op;
				$.ajax({
					dataType: "JSON",
					url:'/addInstance/{{ vmServer.id }}/', //请求地址
					type:"POST",  //提交类似
					data:server_data,  //提交参数
					success:function(response){
						btnObj.removeAttr('disabled');
						if (response["code"] == 200){
							window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);
						}
						else {
							window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
						}
						
					},
			    	error:function(response){
			    		btnObj.removeAttr('disabled');
			    		window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
			    	}
				})	
			}			
			
		</script>

{% endblock %}
